package com.yonyou.crm.common.biz.service;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.crm.common.dao.itf.IBaseDao;
import com.yonyou.crm.common.page.entity.Page;
import com.yonyou.iuap.context.InvocationInfoProxy;

@Service
public abstract class BaseBizServiceImpl<T extends Serializable> {
		
	@Autowired
	protected IBaseDao dao;
	
	/**
	 * 新增
	 * @param vo
	 * @return
	 */
	public T saveVO(T vo) {
		//新增前
		this.beforeSaveVO(vo);
		//新增
		vo = dao.save(vo);
		//新增后
		this.afterSaveVO(vo);
		return vo;
	}
	
	/**
	 * 修改
	 * @param vo
	 * @return
	 */
	public T updateVO(T vo) {
		//修改前
		this.beforeUpdateVO(vo);
		//修改
		vo = dao.update(vo);
		//修改后
		this.afterUpdateVO(vo);
		return vo;
	}
	
	/**
	 * 删除
	 * @param vo
	 * @return
	 */
	public int deleteVO(T vo) {
		//删除前
		this.beforeDeleteVO(vo);
		//删除
		int num=dao.delete(vo);
		//删除后
		this.afterDeleteVO(vo);
		return num;
	}
	
	/**
	 * 按照id删除 
	 * @param clazz 删除类型
	 * @param ids id数组
	 */
	public int batchDeleteByIDs(Class<T> clazz, Object ids) {
		return dao.batchDelete(clazz, ids);
	}
	
	
	/**
	 * 分页
	 * @param clazz
	 * @param p
	 * @param attrs
	 * @param values
	 * @return
	 */
	public Page<T> pageSelect(Class<T> clazz, Page<?> p, String[] attrs, Object[] values) {
		return dao.pageSelect(clazz, p, attrs, values);
	}
	
	/**
	 * 分页统计
	 * @param clazz
	 * @param attrs
	 * @param values
	 * @return
	 */
	public int pageCount(Class<T> clazz,String[] attrs, Object[] values) {
		return dao.pageCount(clazz, attrs, values);
	}
	
	/**
	 * 根据id查询单个VO
	 * @param clazz
	 * @param id
	 * @return
	 */
	public T getVOById(Class<T> clazz,Serializable id) {
		return dao.getById(clazz, id);
	}
	
	protected void beforeSaveVO(T vo) {

		if (vo != null) {

			Long userId = new Long(InvocationInfoProxy.getParameter("id"));
			Date date = new Date();

			try {
				Field tenantId = vo.getClass().getDeclaredField("tenantId");
				if (tenantId != null) {
					tenantId.setAccessible(true);
					tenantId.set(vo, new Long(InvocationInfoProxy.getTenantid()));
				}
			} catch (Exception e) {}
			
			try {
				
				Field sysCreatedUserId = vo.getClass().getDeclaredField("sysCreatedUserId");
				if (sysCreatedUserId != null) {
					sysCreatedUserId.setAccessible(true);
					sysCreatedUserId.set(vo, userId);
				}
				
				Field sysCreatedTime = vo.getClass().getDeclaredField("sysCreatedTime");
				if (sysCreatedTime != null) {
					sysCreatedTime.setAccessible(true);
					sysCreatedTime.set(vo, date);
				}
				
				Field sysModifiedUserId = vo.getClass().getDeclaredField("sysModifiedUserId");
				if (sysModifiedUserId != null) {
					sysModifiedUserId.setAccessible(true);
					sysModifiedUserId.set(vo, userId);
				}
				
				Field sysModifiedTime = vo.getClass().getDeclaredField("sysModifiedTime");
				if (sysModifiedTime != null) {
					sysModifiedTime.setAccessible(true);
					sysModifiedTime.set(vo, date);
				}
			} catch (Exception e) {}
			
			try {
								
				Field enableUserId = vo.getClass().getDeclaredField("enableUserId");
				if (enableUserId != null) {
					enableUserId.setAccessible(true);
					enableUserId.set(vo, userId);
				}
				
				Field enableTime = vo.getClass().getDeclaredField("enableTime");
				if (enableTime != null) {
					enableTime.setAccessible(true);
					enableTime.set(vo, date);
				}
			} catch (Exception e) {}
		}
	}
	protected void afterSaveVO(T vo) {
	}
	protected void beforeUpdateVO(T vo) {
		
		if (vo != null) {
			
			try {
				
				Long userId = new Long(InvocationInfoProxy.getParameter("id"));
				Date date = new Date();

				Field sysModifiedUserId = vo.getClass().getDeclaredField("sysModifiedUserId");
				if (sysModifiedUserId != null) {
					sysModifiedUserId.setAccessible(true);
					sysModifiedUserId.set(vo, userId);
				}
				
				Field sysModifiedTime = vo.getClass().getDeclaredField("sysModifiedTime");
				if (sysModifiedTime != null) {
					sysModifiedTime.setAccessible(true);
					sysModifiedTime.set(vo, date);
				}

			} catch (Exception e) {}
		}
	}
	protected void afterUpdateVO(T vo) {
	}
	protected void beforeDeleteVO(T vo) {
		
		if (vo != null) {
			
			try {
				
				Long userId = new Long(InvocationInfoProxy.getParameter("id"));
				Date date = new Date();

				Field deletedUserId = vo.getClass().getDeclaredField("deletedUserId");
				if (deletedUserId != null) {
					deletedUserId.setAccessible(true);
					deletedUserId.set(vo, userId);
				}
				
				Field deletedTime = vo.getClass().getDeclaredField("deletedTime");
				if (deletedTime != null) {
					deletedTime.setAccessible(true);
					deletedTime.set(vo, date);
				}

			} catch (Exception e) {}
		}
	}
	protected void afterDeleteVO(T vo) {
	}
}
